﻿using NiuX.Dapper.Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;

namespace NiuX.Dapper.Repositories;


public partial class DapperRepository<TEntity>
{
    public Task<TEntity> GetAsync(object id, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return DbConnection.GetAsync<TEntity>(id, transaction, commandTimeout);
    }

    public Task<TEntity> FirstAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return DbConnection.QueryFirstAsync<TEntity>(sql, null, transaction, commandTimeout);
    }

    public Task<TEntity> FirstAsAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        throw new NotImplementedException();
    }

    public async Task<TEntity> FirstOrDefaultAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return (await QueryAsync<TEntity>(sql, null, transaction, commandTimeout)).FirstOrDefault();
    }
}
